package com.jn.zuojia;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jn.server.IStudentChangeListener;
import com.jn.server.IStudentService;
import com.jn.server.Student;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTvCount;
    private EditText mEtName;
    private EditText mEtNumber;
    private EditText mEtGrade;
    private EditText mEtAge;
    private EditText mEtRmNumber;
    private Button mBtnAdd;
    private Button mBtnRemove;
    private Button mBtnQuery;
    private IStudentService mService;
    private int mStudentCount;

    private EditText mEtNameA;
    private EditText mEtNumberA;
    private EditText mEtGradeA;
    private EditText mEtAgeA;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvCount = findViewById(R.id.count);
        mEtName = findViewById(R.id.name);
        mEtNumber = findViewById(R.id.number);
        mEtGrade = findViewById(R.id.grade);
        mEtAge = findViewById(R.id.age);
        mEtRmNumber = findViewById(R.id.rm_number);
        mBtnAdd = findViewById(R.id.add);
        mBtnRemove = findViewById(R.id.remove);
        mBtnQuery = findViewById(R.id.remova);

        // 新增的显示查询结果的 EditText
        mEtNameA = findViewById(R.id.namea);
        mEtNumberA = findViewById(R.id.numbera);
        mEtGradeA = findViewById(R.id.gradea);
        mEtAgeA = findViewById(R.id.agea);

        mTvCount.setOnClickListener(this);
        mBtnAdd.setOnClickListener(this);
        mBtnRemove.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        bindService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }

    private void bindService() {
        Intent intent = new Intent();
        intent.setPackage("com.jn.server");
        intent.setAction("com.jn.server.action");
        startService(intent);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IStudentService.Stub.asInterface(service);
            try {
                mService.addListener(mListener);
                mStudentCount = mService.getStudentCount();
                mTvCount.setText(String.valueOf(mStudentCount));
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };

    IStudentChangeListener mListener = new IStudentChangeListener.Stub() {
        @Override
        public void onStudentCountChanged(int count) throws RemoteException {
            mStudentCount = count;
            mTvCount.setText(String.valueOf(mStudentCount));
        }
    };

    @Override
    public void onClick(View v) {
        if (mService == null) {
            return;
        }
        int id = v.getId();
        if (id == R.id.add) {
            addStudent();
        } else if (id == R.id.remove) {
            removeStudent();
        } else if (id == R.id.count) {
            updateStudentCount();
        } else if (id == R.id.remova) {
            queryStudent();
        }
    }

    private void addStudent() {
        try {
            String name = mEtName.getText().toString();
            int number = Integer.parseInt(mEtNumber.getText().toString());
            String grade = mEtGrade.getText().toString();
            int age = Integer.parseInt(mEtAge.getText().toString());
            Student student = new Student(name, number, grade, age);
            mService.addStudent(student);
        } catch (RemoteException | NumberFormatException e) {
            throw new RuntimeException(e);
        }
    }

    private void removeStudent() {
        try {
            int rm_number = Integer.parseInt(mEtRmNumber.getText().toString());
            mService.deleteStudent(rm_number);
        } catch (RemoteException | NumberFormatException e) {
            throw new RuntimeException(e);
        }
    }

    private void updateStudentCount() {
        try {
            mTvCount.setText(String.valueOf(mService.getStudentCount()));
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    private void queryStudent() {
        String queryNumberStr = mEtRmNumber.getText().toString();
        if (queryNumberStr.isEmpty()) {
            Toast.makeText(this, "请先输入学号", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            int queryNumber = Integer.parseInt(queryNumberStr);
            List<Student> students = mService.getStudentList();
            boolean found = false;

            for (Student student : students) {
                if (student.getNumber() == queryNumber) {
                    mEtNameA.setText(student.getName());
                    mEtNumberA.setText(String.valueOf(student.getNumber()));
                    mEtGradeA.setText(student.getGrade());
                    mEtAgeA.setText(String.valueOf(student.getAge()));
                    found = true;
                    break;
                }
            }

            if (!found) {
                Toast.makeText(this, "没有该学号的信息", Toast.LENGTH_SHORT).show();
                clearQueryResults();
            }
        } catch (RemoteException | NumberFormatException e) {
            throw new RuntimeException(e);
        }
    }

    private void clearQueryResults() {
        mEtNameA.setText("");
        mEtNumberA.setText("");
        mEtGradeA.setText("");
        mEtAgeA.setText("");
    }
}
